package com.atshiyou.dao.impl;

import com.atshiyou.dao.ZanNumDao;
import com.atshiyou.util.DruidUtil;
import com.sun.xml.internal.ws.policy.EffectiveAlternativeSelector;

import java.sql.*;

public class ZanNumDaoImpl extends DruidUtil implements ZanNumDao {
    @Override
    public int getNum(){
        Connection connection = getConnection();
        Statement statement = null;
        ResultSet resultSet = null;
        int number = 0;
        try {
            statement = connection.createStatement();
            resultSet = statement.executeQuery("select number from zannum");
            while (resultSet.next()){
                number = resultSet.getInt("number");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(connection,statement,resultSet);
        }
        return number;
    }

    @Override
    public boolean updateNum(int num) {
        Connection connection = getConnection();
        Statement statement = null;
        PreparedStatement preparedStatement = null;
        int number = 0;
        try {
            preparedStatement =
                    connection.prepareStatement("update zannum set number = ?");
            preparedStatement.setInt(1,num);
            number = preparedStatement.executeUpdate();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            try {
                connection.close();
                preparedStatement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

        }
        if(number==1){
            return true;
        }else
        return false;
    }
}
